$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	/***************************    广告主列表页    ***************************/
	//获取行业
	if($("#industryId").val() == 0 || $("#industryId").val() == ""){
		$("#selectBar_industry a:first").addClass("selectBar_change");
	}
	$.ajax({
		url: ctx+"/widget/selector/provide/industry?parentId="+$("#sessionagentId").val(),
		type: "post",
		contentType: "application/json",
		dataType:"json",
		success: function (data) {
			$.each( data.allIndustry, function( key, val ){
				if($("#industryId").val() == val.id){
				   $("#selectBar_industry .son_selectBar").append('<a value='+ val.id +' class="selectBar_change">'+val.name+'</a>');
				}else{
				   $("#selectBar_industry .son_selectBar").append('<a value='+ val.id +'>'+val.name+'</a>');
				}
			});
		}
	});
			
	$("#selectBar_industry a").live("click",function(){
		$("#industryId").val($(this).attr("value"));
		refreshData();
	});
	
	
	$("selectIndustry").click(function(){
		layer.open({
		    type: 2,
			title: '选择行业',
			shadeClose: false,
			shade: 0.6,
			fix:false,
			area: ['680px','430px'],
			content: [ctx+'/advertiser/main/ad/advertiser/getAllFatherIndustry', 'no']
		}); 
	});
	
 	//状态全部、开启、停用
 	if($("#status").val() == 2 || $("#status").val() == ""){
 		$("#selectBar_status a:first").addClass("selectBar_change");
	}
 	$("#selectBar_status a").each(function(){
 		var value = $(this).attr("value");
 		if($("#status").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
 	$("#selectBar_status a").click(function(){
 		$("#status").val($(this).attr("value"));
 		refreshData();
 	});
 
 	//开启,暂停
 	$('.switch input').change(function(){
		if( $(this).is(':checked') ){
			//$(this).parent().addClass('switPlay'); // 双重保险，但IE低版本不兼容css3的波浪号时可以通过switPlay控制
			url = ctx+"/advertiser/main/ad/advertiser/enable";
			$(this).parent().siblings('.switchText').html('开启');
		}else{
			url = ctx+"/advertiser/main/ad/advertiser/disable";
			$(this).parent().siblings('.switchText').html('停用');
		}
		singleOper($(this),url);
	});
	
	//删除
	$(".btn-delete").click(function(){
		url = ctx+"/advertiser/main/ad/advertiser/delete";
		singleOper($(this),url);
	});
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable,#all-btn-delete").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/advertiser/main/ad/advertiser/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/advertiser/main/ad/advertiser/disable";
		}else if($(this).attr("id") == "all-btn-delete"){
			url = ctx+"/advertiser/main/ad/advertiser/delete";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑广告主    ***************************/
	
	//校验所属行业
	$("#selectIndustry").click(function(){
		layer.open({
		    type: 2,
			title: '选择父行业',
			shadeClose: false,
			shade: 0.6,
			fix:false,
			area: ['680px','430px'],
			content: [ctx+'/advertiser/main/ad/advertiser/getAllFatherIndustry', 'no']
		});
 	});
	
	//初始化加载上传控件
	if($("#cpiPath_div").html()){
		uploadFileHandler("icp_input_id","cpiPath","cpiPathUploadfilestr","cpiPath_div");
	}
 	if($("#licencePath_div").html()){
 		uploadFileHandler("licencePath_input_id","licencePath","licencePathUploadfilestr","licencePath_div");
	}
 	if($("#idPath_div").html()){
 		uploadFileHandler("idPath_input_id","idPath","idPathUploadfilestr","idPath_div");
	}
 	if($("#orgPath_div").html()){
 		uploadFileHandler("orgPath_input_id","orgPath","orgPathUploadfilestr","orgPath_div");
	}
 	
 	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
	$(".btn-save").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		if($("#industryId").val() == "" || $("#industryId").val() == 0 ){
			$("#industryul_selectdiv").find('span.spanVaildate').html("请选择行业");
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$(".btn-cancel").click(function(){
		location.href = ctx+"/advertiser/main/ad/advertiser/list";
	});
	
});
		
function exportExcel_list(){
	var searchValue = $("#searchValue").val();
	if(searchValue == "请输入关键字"){
		searchValue = "";
	}
	var param = "?industryId="+$("#industryId").val()+"&status="+$("#status").val()+"&searchValue="+searchValue;
	$("#exportExcel").attr("href",$("#ctx").val()+"/advertiser/main/ad/advertiser/exportDataReportExcel"+param);
	return false;
}

function uploadFileHandler(elemIdObjId,objId,fileStrObj,objDivId){
	initFile($("#"+elemIdObjId),$("#"+objId).val());
	if($("#"+objId).val() != "" && $("#"+objId).val() != "noFiles"){
 		$('#'+fileStrObj).html("预览");
 	}
	$('#'+fileStrObj).unbind('click').click(function() {
		if($(this).html() == "上传"){
			getUploadFile($(this),elemIdObjId,objDivId,objId,null,null,null);
		}else if($(this).html() == "预览"){
			if($("#"+objId).val() != ""){
				bootbox.dialog({
				message:'<img src="'+$("#"+objId).val()+'"/>',
				title: "浏览素材",
				locale: 'zh_CN'
			});
			}
		}
	});
}